
#include "../headers/CLERK.h"


//进货系统，查询进货信息(并可实现查询两个进行对比)，写入进货信息，打印进货信息
void restock() {
    struct time_item now;
    get_timeItem(&now);
    pointer *h = pointer_create(), *head, *h0 = pointer_create();
    purchaseMessage *p;
    print_menu("[1]查询进货记录\t[2]录入进货信息\t[3]打印进货信息\t[4]回到理货员主页");
    int mode, print_check, print_num, print_again;
    while (!input_int(&mode, 1, 4));
    switch (mode) {
        case 1:
            restock_find(head_purchaseMessage, h);
            for(;;) {
                print_menu("[1]继续查询\t[2]回到进货系统主页\t[3]回到理货员主页");
                int next;
                while (!input_int(&next, 1, 3));
                if(next==1) {
                    pointer *lala =pointer_create();
                    restock_find(head_purchaseMessage, lala);
                } else if (next == 2) {
                    restock();
                    return;
                } else return;
            }
        case 2:
            restock_input(head_purchaseMessage);
            return;
        case 3:
            printf("请选择要打印多少天[1]一周  [2]一个月(30天)  [3]全部  [4]自定义天数：");
            while (!input_int(&print_check, 1, 4));
            printf("请选择要以何种顺序打印[1]时间从远到近  [2]数量由多到少  [3]总价由多到少：");
            while (!input_int(&print_again, 1, 3));
            switch (print_check) {
                case 1:
                    print_num = 7;
                    break;
                case 2:
                    print_num = 30;
                    break;
                case 3: {
                    if (print_again == 1) {
                        for (p = head_purchaseMessage->next; p != NULL; p = p->next)
                            pointer_add(h, p, 1, get_dayPassed(p->purchaseTime, now));
                    } else if (print_again == 2) {
                        for (p = head_purchaseMessage->next; p != NULL; p = p->next)
                            pointer_add(h, p, 1, p->kilo);
                    } else {
                        for (p = head_purchaseMessage->next; p != NULL; p = p->next)
                            pointer_add(h, p, 1, p->totalPrice);
                    }
                    pointer_sort(h, 1);
                    print_purchase(h);
                    print_num = 0;
                }
                    break;
                case 4:
                    printf("请输入您要打印的天数：");
                    while (!input_int(&print_num, 0, 114514));
            }
            if (print_num != 0) {
                for (p = head_purchaseMessage->next; p != NULL; p = p->next)
                    pointer_add(h, p, 1, get_dayPassed(p->purchaseTime, now));
                pointer_sort(h, 0);
                head = h;
                h0->next = h;
                for (h = head->next; h != NULL; h = h->next) {
                    h0 = h0->next;
                    if (h->temp > print_num || h->temp < 0) {
                        h0->next = NULL;
                        break;
                    }
                }
                if (head->next == NULL) printf("距今%d天内无进货记录\n", print_num);
                else {
                    if (print_again == 2) {
                        for (h = head->next; h != NULL; h = h->next)
                            h->temp = h->p_purchase->kilo;
                    }
                    if (print_again == 3) {
                        for (h = head->next; h != NULL; h = h->next)
                            h->temp = h->p_purchase->totalPrice;
                    }
                    pointer_sort(head, 1);
                    print_purchase(head);
                }
            }
            break;
        case 4:
            return;
    }
    print_menu("[1]继续在进货系统工作\t[2]回到理货员主页");
    int next_way;
    while (!input_int(&next_way, 1, 2));
    switch (next_way) {
        case 1:
            restock();
            break;
        case 2:
            break;
    }
    return;
}

//查询进货信息
void restock_find(purchaseMessage *head, pointer *h) {
    purchaseMessage *p;
    struct time_item now;
    bool flag = false;
    get_timeItem(&now);
    print_menu("[1]名称/品种\t[2]进货商\t[3]进货时间");
    int mode;
    while (!input_int(&mode, 1, 3));
    switch (mode) {
        case 1: {
            printf("请输入名称/品种：");
            char keyword[64];
            while (!input_string(keyword, 64));
            for (p = head->next; p != NULL; p = p->next) {
                if (strcmp(keyword, p->name) == 0 || strcmp(keyword, p->type) == 0) {
                    flag = true;
                    pointer_add(h, p, 1, get_dayPassed(p->purchaseTime, now));
                    pointer_sort(h, 1);
                }
            }
            if (flag) {
                printf("以下查询结果按时间从远到近打印\n");
                print_purchase(h);
            }
        }
            break;
        case 2: {
            printf("请输入进货商名称：");
            char keyword[64];
            while (!input_string(keyword, 64));
            for (p = head->next; p != NULL; p = p->next) {
                if (strcmp(keyword, p->supplier) == 0) {
                    flag = true;
                    pointer_add(h, p, 1, p->kilo);
                }
            }
            if (flag) print_purchase(h);
        }
            break;
        case 3: {
            int year, month, day;
            printf("请输入要查询的时间(YYYY/MM/DD): ");
            while (1) {
                if (scanf("%d/%d/%d", &year, &month, &day) == 3) {
                    while (getchar() != '\n');
                    break;
                } else {
                    while (getchar() != '\n');
                    puts("输入错误 请重新输入：");
                }
            }
            for (p = head->next; p != NULL; p = p->next) {
                if (p->purchaseTime.year == year && p->purchaseTime.month == month && p->purchaseTime.day == day) {
                    flag = true;
                    pointer_add(h, p, 1, p->kilo);
                }
            }
            if (flag) print_purchase(h);
        }
            break;
    }
    if (!flag)printf("查无此商品进货记录！请重新查询\n");
}

//录入进货记录
void restock_input(purchaseMessage *head) {
    struct time_item lifeTime, now;
    get_timeItem(&now);
    int input_way=0,choice=0,input_do=0,next=0;
    pointer *h = pointer_create(),*h0=pointer_create();
    purchaseMessage *head_data = purchaseMessage_create(),*head2=NULL,*p;
    printf("请选择录入方式 [1]直接录入  [2]从其他文件录入：");
    while (!input_int(&input_way, 1, 2));
    if (input_way == 2) {
        printf("请输入您要录入的文件路径(请以../XX/XXX输入)：");
        char path[128];
        while (!input_string(path, 128));
        if (!isFileOK(path)) {
            printf("❓ 路径不存在，要重新查找吗?\n");
            for (;;) {
                print_menu("[1]继续查找\t[2]不查了,直接录\t[3]不录了");
                while (!input_int(&choice, 1, 3));
                if (choice == 1) {
                    printf("请输入您要录入的文件路径(请以../XX/XXX输入)：");
                    while (!input_string(path, 128));
                    if (!isFileOK(path))
                        printf("❓ 路径不存在，要重新查找吗?\n");
                    else {
                        read_purchaseMessage(path, head_data);
                        break;
                    }
                }
                if (choice == 2) break;
                if (choice == 3) return;
            }
        } else read_purchaseMessage(path, head_data);
        if (choice != 2) {
            printf("您要[1]全部录入  [2]部分录入：");
            while (!input_int(&input_do, 1, 2));
            if (input_do == 1) {
                for (head2 = head_purchaseMessage->next; head2->next != NULL; head2 = head2->next);
                head2->next = head_data->next;
                for (p = head_data->next; p != NULL; p = p->next) pointer_add(h0, p, 1, p->kilo);
            } else {
                for (p = head_data->next; p != NULL; p = p->next) pointer_add(h, p, 1, p->kilo);
                print_purchase(h);
                printf("请输入你要录入的进货商品编号：");
                pointer *number_check = h;
                int number = 0, input_number = 0;
                for (; number_check->next != NULL; number_check = number_check->next) { number++; }
                while (!input_int(&input_number, 1, number));
                for (int j = 1; j <= input_number; j++) h = h->next;
                h->p_purchase->next = NULL;
                for (head2 = head_purchaseMessage->next; head2->next != NULL; head2 = head2->next);
                head2->next = h->p_purchase;
                pointer_add(h0, h->p_purchase, 1, 0.0);
            }
            printf("以下为此次录入的商品\n");
            print_purchase(h0);
        }
        print_menu("[1]回到进货系统主页\t[2]回到理货员主页");
        while (!input_int(&next, 1, 2));
        switch (next) {
            case 1:
                restock();
                return;
            case 2:
                return;
        }
    }
    if(input_way==1||(input_way==2&&choice==2)) {
        printf("请录入以下信息：名称，品种，进货单价，进货数量，进货商，保质期\n"); //总价由计算得到，进货时间默认为录入时间
        int due;
        fruit input_goods_storage;
        purchaseMessage input_goods;
        printf("请输入录入的商品名称：");
        while (!input_string(input_goods.name, 64));
        printf("请输入录入的的商品品种：");
        while (!input_string(input_goods.type, 64));
        printf("请输入录入的商品进货单价：");
        while (!input_double(&input_goods.unitPrice));
        printf("请输入录入的商品进货数量：");
        while (!input_double(&input_goods.kilo));
        printf("请输入录入商品的进货商：");
        while (!input_string(input_goods.supplier, 64));
        printf("请输入录入的商品的保质期：");
        while (!input_int(&due, 0, 0));
        input_goods.purchaseTime = now;
        input_goods.totalPrice = input_goods.kilo * input_goods.unitPrice;
        purchaseMessage_add(head_purchaseMessage, input_goods);
        pointer_add(h, &input_goods, 1, 0.00);
        printf("以下为此次录入信息：\n");
        print_purchase(h);
        strcpy(input_goods_storage.name, input_goods.name);
        strcpy(input_goods_storage.type, input_goods.type);
        input_goods_storage.kilo = input_goods.kilo;
        input_goods_storage.unitPrice = input_goods.unitPrice;
        get_dateAfter(now, due, &lifeTime);
        input_goods_storage.life = lifeTime;
        input_goods_storage.lifetime = due;
        fruit_add(head_fruitStored, input_goods_storage);
        print_menu("[1]我要继续录入\t[2]回进货系统\t[3]回到理货员初始页面");
        int input_after_act;
        while (!input_int(&input_after_act, 1, 3));
        switch (input_after_act) {
            case 1:
                restock_input(head);
                return;
            case 2:
                restock();
                return;
            case 3:
                return;
        }
    }
}

//库存管理系统，提醒仓库中即将过期水果
void storage() {
    struct time_item now;
    get_timeItem(&now);
    fruit *p;
    pointer *h = pointer_create(), *head, *h0 = pointer_create();
    print_menu("[1]查询仓库库存\t[2]打印仓库库存\t[3]显示临近过期水果\t[4]回到理货员主页");
    int mode, next, print_check, print_again, print_num;
    while (!input_int(&mode, 1, 4));
    switch (mode) {
        case 1:
            storage_find(head_fruitStored, h);
            for (;;) {
                print_menu("[1]继续查询\t[2]回到库存管理系统系统主页\t[3]回到理货员主页");
                while (!input_int(&next, 1, 3));
                if (next == 1) {
                    h = pointer_create();
                    storage_find(head_fruitStored, h);
                } else if (next == 2) {
                    pointer_free(h);
                    storage();
                    return;
                } else return;
            }
        case 2:
            printf("请选择要打印到期距今天多久[1]一周  [2]一个月(30天)  [3]全部  [4]自定义天数：");
            while(!input_int(&print_check, 1, 4));
            printf("请选择要以何中顺序打印[1]到期时间远近(按天)  [2]数量由多到少  [3]单价由多到少：");
            while(!input_int(&print_again, 1, 3));
            switch (print_check) {
                case 1:
                    print_num = 7;
                    break;
                case 2:
                    print_num = 30;
                    break;
                case 3: {
                    if (print_again == 1) {
                        for (p = head_fruitStored->next; p != NULL; p = p->next)
                            pointer_add(h, p, 0, get_dayPassed(now, p->life));
                    } else if (print_again == 2) {
                        for (p = head_fruitStored->next; p != NULL; p = p->next)
                            pointer_add(h, p, 0, -p->kilo);
                    } else {
                        for (p = head_fruitStored->next; p != NULL; p = p->next)
                            pointer_add(h, p, 0, -p->unitPrice);
                    }
                    pointer_sort(h, 0);
                    print_fruitStored(h);
                    print_num = 0;
                }
                    break;
                case 4:
                    printf("请输入您要打印的天数：");
                    while (!input_int(&print_num, 0, 114514));
            }
            if (print_num != 0) {
                for (p = head_fruitStored->next; p != NULL; p = p->next)
                    pointer_add(h, p, 0, get_dayPassed(now, p->life));
                pointer_sort(h, 1);
                head = h;
                h0->next = h;
                for (h = head; h != NULL; h = h->next) {
                    if (h->temp > print_num || h->temp < 0) {
                        h0->next = NULL;
                        break;
                    }
                    h0 = h0->next;
                }
                if (head->next == NULL) {
                    printf("暂无在距今%d天过期的库存\n", print_num);
                } else {
                    if (print_again == 2) {
                        for (h = head->next; h != NULL; h = h->next)
                            h->temp = h->p_fruit->kilo;
                    }
                    if (print_again == 3)
                        for (h = head->next; h != NULL; h = h->next)
                            h->temp = h->p_fruit->unitPrice;
                    pointer_sort(head, 1);
                    print_fruitStored(head);
                }
            }
            print_menu("[1]继续在库存系统工作\t[2]回到理货员主页");
            while (!input_int(&next, 1, 2));
            switch (next) {
                case 1:
                    storage();
                    return;
                case 2:
                    return;
            }
        case 3:
            h = overdue_check(3, head_fruitStored, h);
            if (h->next == NULL) {
                print_menu("[1]继续在库存管理系统\t[2]回到理货员主页");
                while (!input_int(&next, 1, 2));
                switch (next) {
                    case 1:
                        storage();
                        return;
                    case 2:
                        return;
                }
            }
            overdue_act(h, 3);
        case 4:
            return;
    }
}

//查询仓库信息
pointer *storage_find(fruit *head, pointer *h) {
    fruit *p;
    bool flag = false;
    print_menu("[1]名称/品种\t[2]单价\t[3]临过期库存");
    int mode;
    while (!input_int(&mode, 1, 3));
    switch (mode) {
        case 1: {
            printf("请输入名称/品种：");
            char keyword[64];
            while (!input_string(keyword, 64));
            for (p = head->next; p != NULL; p = p->next) {
                if (strcmp(keyword, p->name) == 0 || strcmp(keyword, p->type) == 0) {
                    flag = true;
                    pointer_add(h, p, 0, 0.00);
                }
            }
        }
            break;
        case 2: {
            printf("请输入单价：");
            double key;
            while (!input_double(&key));
            for (p = head->next; p != NULL; p = p->next) {
                if (key == p->unitPrice) {
                    flag = true;
                    pointer_add(h, p, 0, 0.00);
                }
            }
        }
            break;
        case 3:
            h = overdue_check(3, head_fruitStored, h);
            return h;
    }
    if (flag) print_fruitStored(h);
    else printf("查无此商品进货记录！\n");
    return h;
}

//上架系统，上架水果，设置更改信息
void shelf() {
    struct time_item now;
    get_timeItem(&now);
    pointer *h = pointer_create(), *head, *h0 = pointer_create();
    fruit *p;
    print_menu("[1]打印货架上的商品\t[2]更改架上的商品信息\t[3]显示临近过期商品\t[4]查询架上商品信息\t[5]上架水果\t[6]回到理货员主页");
    int mode, next, print_check, print_again, print_num;
    while (!input_int(&mode, 1, 6));
    switch (mode) {
        case 1:
            printf("请选择距今多少天上架[1]一周  [2]一个月(30天)  [3]全部  [4]自定义天数：");
            while(!input_int(&print_check, 1, 4));
            printf("请选择要以何中顺序打印[1]上架时间远近  [2]数量由多到少  [3]单价由多到少：");
            while(!input_int(&print_again, 1, 3));
            switch (print_check) {
                case 1:
                    print_num = 7;
                    break;
                case 2:
                    print_num = 30;
                    break;
                case 3: {
                    if (print_again == 1) {
                        for (p = head_fruitOnShelf->next; p != NULL; p = p->next){
                            pointer_add(h, p, 0, get_dayPassed(p->shelfTime, now));}
                    }
                    if (print_again == 2) {
                        for (p = head_fruitOnShelf->next; p != NULL; p = p->next)
                            pointer_add(h, p, 0, p->kilo);
                    }
                    if( print_again==3 ){
                        for (p = head_fruitOnShelf->next; p != NULL; p = p->next)
                            pointer_add(h, p, 0, p->unitPrice);
                    }
                    pointer_sort(h, 1);
                    print_fruitOnShelf(h);
                    print_num = 0;
                }
                    break;
                case 4:
                    printf("请输入您要打印的天数：");
                    while (!input_int(&print_num, 0, 114514));
            }
            if (print_num != 0) {
                for (p = head_fruitOnShelf->next; p != NULL; p = p->next)
                    pointer_add(h, p, 0, get_dayPassed(now, p->shelfTime));
                pointer_sort(h, 1);
                head = h;
                h0->next = h;
                for (h = head; h != NULL; h = h->next) {
                    if (h->temp < -print_num || h->temp > 0) {
                        h0->next = NULL;
                        break;
                    }
                    h0 = h0->next;
                }
                if (head->next == NULL)   printf("货架暂无%d天内上架的商品\n", print_num);
                 else {
                    if (print_again == 2) {
                        for (h = head->next; h != NULL; h = h->next)
                            h->temp = -h->p_fruit->kilo;
                    }
                    if (print_again == 3)
                        for (h = head->next; h != NULL; h = h->next)
                            h->temp = -h->p_fruit->unitPrice;
                    pointer_sort(head, 0);
                    print_fruitOnShelf(head);
                 }
            }
            print_menu("[1]继续在上架系统工作\t[2]回到理货员主页");
            while (!input_int(&next, 1, 2));
            switch (next) {
                case 1:
                    shelf();
                    return;
                case 2:
                    return;
            }
            break;
        case 2:
            shelf_change(head_fruitOnShelf);
            return;
        case 3:
            h = overdue_check(2, head_fruitOnShelf, h);
            if (h->next == NULL) break;
            overdue_act(h, 2);
            return;
        case 4:
            shelf_find(head_fruitOnShelf, h);
            for (;;) {
                print_menu("[1]继续查询\t[2]回到上架系统系统主页\t[3]回到理货员主页");
                while (!input_int(&next, 1, 3));
                if (next == 1) {
                    h = pointer_create();
                    shelf_find(head_fruitOnShelf, h);
                } else if (next == 2) {
                    pointer_free(h);
                    shelf();
                    return;
                } else return;
            }
        case 5:
            h = storage_find(head_fruitStored, h);
            if (h->next == NULL) {
                print_menu("[1]重新查询再上架\t[2]回到上架系统主页\t[3]回到理货员主页");
                while (!input_int(&next, 1, 3));
                if (next == 1) storage_find(head_fruitStored, h);
                else if (next == 2) {
                    shelf();
                    return;
                } else return;
            }
            shelf_put(head_fruitStored, head_fruitOnShelf, h);
            return;
        case 6:
            return;
    }
    print_menu("[1]继续在上架系统\t[2]回到理货员主页");
    while (!input_int(&next, 1, 2));
    if (next == 1) {
        pointer_free(h);
        shelf();
    }
    return;
}

//查询货架信息
pointer *shelf_find(fruit *head, pointer *h) {
    fruit *p;
    bool flag = false;
    print_menu("[1]名称/品种\t[2]价格模式\t[3]等级\t[4]上架时间\t[5]临过期商品");
    int mode;
    while (!input_int(&mode, 1, 5));
    switch (mode) {
        case 1: {
            printf("请输入名称/品种：");
            char keyword[64];
            while (!input_string(keyword, 64));
            for (p = head->next; p != NULL; p = p->next) {
                if (strcmp(keyword, p->name) == 0 || strcmp(keyword, p->type) == 0) {
                    flag = true;
                    pointer_add(h, p, 0, p->kilo);
                }
            }
        }
            break;
        case 2: {
            printf("请输入价格模式([1]零售,[2]促销,[3]批发,[4]赠送)：");
            int key;
            while (!input_int(&key, 1, 4));
            for (p = head->next; p != NULL; p = p->next) {
                if ((key - 1) == p->mode) {
                    flag = true;
                    pointer_add(h, p, 0, p->kilo);
                }
            }
        }
            break;
        case 3: {
            printf("请输入商品等级([1]优,[2]中,[3]劣)：");
            int key;
            while (!input_int(&key, 1, 3));
            for (p = head->next; p != NULL; p = p->next) {
                if ((key - 1) == p->level) {
                    flag = true;
                    pointer_add(h, p, 0, p->kilo);
                }
            }
        }
            break;
        case 4: {
            int year, month, day;
            printf("请输入要查询的时间(YYYY/MM/DD): ");
            while (1) {
                if (scanf("%d/%d/%d", &year, &month, &day) == 3) {
                    while (getchar() != '\n');
                    break;
                } else {
                    while (getchar() != '\n');
                    puts("输入错误 请重新输入：");
                }
            }
            for (p = head->next; p != NULL; p = p->next) {
                if (p->shelfTime.year == year && p->shelfTime.month == month && p->shelfTime.day == day) {
                    flag = true;
                    pointer_add(h, p, 0, 0.00);
                }
            }
        }
            break;
        case 5:
            h = overdue_check(2, head_fruitOnShelf, h);
            return h;
    }
    if (flag) print_fruitOnShelf(h);
    else printf("查无此商品货架记录！请重新查找\n");
    return h;
}

//更改货架信息
void shelf_change(fruit *head) {
    printf("请查找货架上的商品：");
    fruit change_item;
    int new_mode, new_level, next, i, way, over_check;
    pointer *h = pointer_create(), *h0 = pointer_create(), *change_h = pointer_create();
    h = shelf_find(head, h);
    pointer *number_check = h;
    int number = 0;
    for (; number_check->next != NULL; number_check = number_check->next) { number++; }
    if (number == 0) {
        print_menu("[1]重新查询再更改\t[2]回到上架系统主页\t[3]回到理货员主页");
        while (!input_int(&next, 1, 3));
        switch (next) {
            case 1:
                shelf_change(head_fruitOnShelf);
                return;
            case 2:
                shelf();
                return;
            case 3:
                return;
        }
    }
    printf("请选择您要修改的编号：");
    while (!input_int(&i, 1, number));
    for (int j = 1; j <= i; j++) {
        h0->next = h;
        h = h->next;
    }
    change_item = *h->p_fruit;
    printf("您要修改[1]多项数据\t[2]单项数据\t[3]删除数据：");
    while (!input_int(&way, 1, 3));
    switch (way) {
        case 1: {
            printf("请输入要修改的数量：");
            while (!input_double(&change_item.kilo));
            printf("请输入新的单价：");
            while (!input_double(&change_item.unitPrice));
            printf("请输入新的价格模式([1]零售[2]促销[3]批发[4]赠送)：");
            while (!input_int(&new_mode, 1, 4));
            printf("请输入新的等级[1]优[2]中[3]劣：");
            while (!input_int(&new_level, 1, 3));
            h->p_fruit->kilo = change_item.kilo;
            h->p_fruit->unitPrice = change_item.unitPrice;
            h->p_fruit->mode = new_mode - 1;
            change_item.mode = new_mode - 1;
            h->p_fruit->level = new_level - 1;
            change_item.level = new_level - 1;
        }
            break;
        case 2: {
            printf("请选择要修改的内容:[1]总量\t[2]单价\t[3]价格模式\t[4]等级：");
            int mode;
            while (!input_int(&mode, 1, 4));
            switch (mode) {
                case 1:
                    printf("请输入新的总量：");
                    input_double(&change_item.kilo);
                    h->p_fruit->kilo = change_item.kilo;
                    break;
                case 2:
                    printf("请输入新的价格：");
                    input_double(&change_item.unitPrice);
                    h->p_fruit->unitPrice = change_item.unitPrice;
                    break;
                case 3:
                    printf("请输入新的价格模式([1]零售,[2]促销,[3]批发,[4]赠送)：");
                    while (!input_int(&new_mode, 1, 4));
                    h->p_fruit->mode = new_mode - 1;
                    change_item.mode = new_mode - 1;
                    break;
                case 4:
                    printf("请输入新的等级([1]优,[2]中,[3]劣)：");
                    while (!input_int(&new_level, 1, 3));
                    h->p_fruit->level = new_level - 1;
                    change_item.level = new_level - 1;
                    break;
            }
        }
            break;
        case 3:
            printf("确认删除?[1]是\t[2]否:");
            while (!input_int(&over_check, 1, 2));
            if (over_check == 1) {
                change_item = *h->p_fruit;
                change_item.next = NULL;
                fruit_del(head_fruitOnShelf, h->p_fruit);
                break;
            } else {
                printf("请重新更改货架上的商品\n");
                shelf_change(head_fruitOnShelf);
                return;
            }
    }
    if (way != 3) printf("以下为修改后的信息：\n");
    else printf("以下为已删除掉信息：\n");
    pointer_add(change_h, &change_item, 0, change_item.kilo);
    print_fruitOnShelf(change_h);
    print_menu("[1]继续查询并修改\t[2]回到上架系统\t[3]回到理货员初始页面");
    while (!input_int(&next, 1, 3));
    switch (next) {
        case 1:
            shelf_change(head);
            return;
        case 2:
            shelf();
            return;
        case 3:
            return;
    }
}

//上架
void shelf_put(fruit *head1, fruit *head2, pointer *h) {
    pointer *head = h, *number_check = h;
    int number = 0;
    for (; number_check->next != NULL; number_check = number_check->next) {
        number++;
    }
    fruit put_item;
    pointer *h0 = pointer_create(), *put_h = pointer_create();
    printf("请选择您要上架库存的编号：");
    int i;
    while (!input_int(&i, 1, number));
    for (int j = 1; j <= i; j++) {
        h0->next = h;
        h = h->next;
    }
    strcpy(put_item.name, h->p_fruit->name);
    strcpy(put_item.type, h->p_fruit->type);
    printf("请输入要上架的数量：");
    while (!input_double(&put_item.kilo));
    if (h->p_fruit->kilo < put_item.kilo) {
        printf("上架数量超过库存，请重新上架!\n");
        print_fruitStored(head);
        shelf_put(head1, head_fruitOnShelf, head);
        return;
    }
    printf("请输入要定的价格：");
    while (!input_double(&put_item.unitPrice));
    int mode, level;
    printf("请输入要定的价格模式([1]零售,[2]促销,[3]批发,[4]赠送)：");
    while (!input_int(&mode, 1, 4));
    printf("请输入要定的等级([1]优,[2]中,[3]劣)：");
    while (!input_int(&level, 1, 3));
    put_item.mode = mode - 1;
    put_item.level = level - 1;
    //读入时间
    struct time_item now;
    get_timeItem(&now);
    put_item.life = h->p_fruit->life;
    put_item.shelfTime = now;
    if (h->p_fruit->kilo == put_item.kilo) {
        fruit_del(head1, h->p_fruit);
        pointer_del(head, h);
    } else {
        h->p_fruit->kilo = h->p_fruit->kilo - put_item.kilo;
    }
    fruit_add(head2, put_item);
    pointer_add(put_h, &put_item, 0, put_item.kilo);
    printf("以下为上架信息：\n");
    print_fruitOnShelf(put_h);
    print_menu("[1]我要继续上架此类商品\t[2]重新查询再上架\t[3]回到库存管理系统\t[4]回到上架系统\t[5]回到理货员初始页面");
    int put_after_act;
    while (!input_int(&put_after_act, 1, 5));
    switch (put_after_act) {
        case 1: {
            if (head->next == NULL) {
                printf("已无此类商品\n");
                print_menu("[1]重新查询再上架\t[2]回到上架系统主页\t[3]回到理货员主页");
                int next;
                while (!input_int(&next, 1, 3));
                switch (next) {
                    case 1: {
                        pointer_free(head);
                        pointer *put_again = pointer_create();
                        put_again = storage_find(head_fruitStored, put_again);
                        if (put_again->next == NULL) {
                            print_menu("[1]重新查询再上架\t[2]回到上架系统主页\t[3]回到理货员主页");
                            int next_do;
                            while (!input_int(&next_do, 1, 3));
                            if (next_do == 1) storage_find(head_fruitStored, put_again);
                            else if (next_do == 2) {
                                shelf();
                                return;
                            } else return;
                        }
                        shelf_put(head_fruitStored, head_fruitOnShelf, put_again);
                    }
                        return;
                    case 2:
                        shelf();
                        return;
                    case 3:
                        return;
                }
            }
            print_fruitStored(head);
            shelf_put(head_fruitStored, head_fruitOnShelf, head);
            return;
        }
        case 2: {
            pointer_free(head);
            pointer *put_again = pointer_create();
            put_again = storage_find(head_fruitStored, put_again);
            if (put_again->next == NULL) {
                print_menu("[1]重新查询再上架\t[2]回到上架系统主页\t[3]回到理货员主页");
                int next;
                while (!input_int(&next, 1, 3));
                if (next == 1) storage_find(head_fruitStored, put_again);
                else if (next == 2) {
                    shelf();
                    return;
                } else return;
            }
            shelf_put(head_fruitStored, head_fruitOnShelf, put_again);
        }
            return;
        case 3: {
            storage();
            return;
        }
        case 4: {
            shelf();
            return;
        }
        case 5:
            return;
    }
    return;
}

//查过期
pointer *overdue_check(int ddl, fruit *head, pointer *q) {
    fruit *p;
    struct time_item now;
    int first_find = 0;
    get_timeItem(&now);
    q = pointer_create();
    for (p = head->next; p != NULL; p = p->next) {
        if (get_dayPassed(now, p->life) <= ddl) {
            first_find++;
            if (first_find == 1) printf("以下为临过期库存：\n");
            pointer_add(q, p, 0, p->kilo);
        }
    }
    if (first_find == 0) printf("暂无临过期商品\n");
    else {
        if (ddl == 2) print_fruitOnShelf(q);
        if (ddl == 3) print_fruitStored(q);
    }
    return q;
}

//对过期商品操作
void overdue_act(pointer *h, int ddl) {
    pointer *head = h, *number_check = h;
    int number = 0;
    for (; number_check->next != NULL; number_check = number_check->next) {
        number++;
    }
    int next_act;
    if (ddl == 2) {
        print_menu("[1]处理掉这些商品\t[2]更改这些商品信息\t[3]回到上架系统主页\t[4]回到理货员主页");
        while (!input_int(&next_act, 1, 4));
        if (next_act == 3) next_act = 6;
        if (next_act == 2) next_act = 5;
    } else {
        print_menu("[1]处理掉这些库存\t[2]上架这些水果（价格模式为促销或赠送且等级为劣）\t[3]回到库存管理系统主页\t[4]回到理货员主页");
        while (!input_int(&next_act, 1, 4));
    }
    switch (next_act) {
        case 1: {
            printf("请选择您要处理的商品编号：");
            int clear, check;
            while (!input_int(&clear, 1, number));
            printf("确认删除？[1]是\t[2]否：");
            while (!input_int(&check, 1, 2));
            if (check == 1) {
                for (int j = 1; j <= clear; j++) h = h->next;
                if (ddl == 3) fruit_del(head_fruitStored, h->p_fruit);
                else fruit_del(head_fruitOnShelf, h->p_fruit);
                pointer_del(head, h);
            }
        }
            break;
        case 2:
            shelf_put(head_fruitStored, head_fruitOnShelf, h);
            break;
        case 3: {
            pointer_free(h);
            storage();
            return;
        }
        case 4:
            return;
        case 5: {
            printf("请选择您要修改的商品：");
            int modify;
            while (!input_int(&modify, 1, number));
            for (int j = 1; j <= modify; j++) h = h->next;
            printf("请输入新的单价：");
            while (!input_double(&h->p_fruit->unitPrice));
            printf("请输入新的价格模式([1]零售,[2]促销,[3]批发,[4]赠送)：");
            int new_mode;
            while (!input_int(&new_mode, 1, 4));
            h->p_fruit->mode = new_mode - 1;
            printf("请输入新的等级([1]优,[2]中,[3]劣)");
            int new_level;
            while (!input_int(&new_level, 1, 3));
            h->p_fruit->level = new_level - 1;
            printf("以下为修改后的信息：\n");
            fruit change_item;
            pointer *change_h = pointer_create();
            change_item = *h->p_fruit;
            pointer_add(change_h, &change_item, 0, change_item.kilo);
            print_fruitOnShelf(change_h);
        }
            break;
        case 6: {
            pointer_free(h);
            shelf();
            return;
        }
    }
    if (head->next == NULL) {
        printf("已无临过期商品！\n");
        print_menu("[1]回到库存管理系统\t[2]回到上架系统\t[3]回到理货员主页");
        int next_do_do;
        while (!input_int(&next_do_do, 1, 3));
        switch (next_do_do) {
            case 1:
                storage();
                return;
            case 2:
                shelf();
                return;
            case 3:
                return;
        }
        return;
    }
    int next_do;
    if (next_act != 2) {
        if (ddl == 2) { print_menu("[1]继续操作\t[2]回到上架系统\t[3]回到理货员主页"); }
        else { print_menu("[1]继续操作\t[2]回到库存管理系统\t[3]回到理货员主页"); }
        while (!input_int(&next_do, 1, 3));
        switch (next_do) {
            case 1:
                if (ddl == 3) print_fruitStored(head);
                else print_fruitOnShelf(head);
                overdue_act(head, ddl);
                return;
            case 2:
                if (ddl == 3) {
                    storage();
                    return;
                } else {
                    shelf();
                    return;
                }
            case 3:
                return;
        }
    }
}